package com.example.wanandroid.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.wanandroid.R;
import com.example.wanandroid.activity.DataContentActivity;
import com.example.wanandroid.adapter.NavigationAdapter;
import com.example.wanandroid.entity.NavigationDataBean;
import com.example.wanandroid.constant.Constant;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.toolbox.Loger;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

public class NavigationFragment extends Fragment {

    RecyclerView mRecyclerView;
    NavigationAdapter baseAdapter;
    List<NavigationDataBean> mList=new ArrayList<>();
    private RefreshLayout refreshLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_navigation, null);

        initData();
        initView(view);
        return view;
    }

    private void initView(View view) {
        refreshLayout = view.findViewById(R.id.refreshLayout);
        //设置 Header 为 贝塞尔雷达 样式
        refreshLayout.setRefreshHeader(new BezierRadarHeader(getActivity()).setEnableHorizontalDrag(true));
        //设置 Footer 为 球脉冲 样式
        refreshLayout.setRefreshFooter(new BallPulseFooter(getActivity()).setSpinnerStyle(SpinnerStyle.Scale));
        mRecyclerView=view.findViewById(R.id.mRecyclerView);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,  StaggeredGridLayoutManager.VERTICAL));
        baseAdapter=new NavigationAdapter(getContext(),mList);
        mRecyclerView.setAdapter(baseAdapter);

        baseAdapter.setOnItemClickListener(new NavigationAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                Intent intent = new Intent(getContext(), DataContentActivity.class);
                intent.putExtra("url", mList.get(position).getLink());
                Log.d("rcl", "==onClick: =="+mList.get(position).getLink());
                intent.putExtra("title", mList.get(position).getName());
                startActivity(intent);
            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {

                refreshlayout.finishRefresh(200/*,false*/);//传入false表示刷新失败
                mList.clear();
                initData();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(200/*,false*/);//传入false表示加载失败
                mList.clear();
                initData();
            }
        });
        refreshLayout.setEnableLoadMore(true);
        //refreshLayout.autoRefresh();
    }

    private void initData() {
        //get请求简洁版实现
        RxVolley.get(Constant.COMMON_WEBSITE, new HttpCallback() {
            @Override
            public void onSuccess(String data) {
                Loger.debug("请求到的数据:" + data);
                if (!TextUtils.isEmpty(data)) {
                    getJson(data);
                }
            }
        });
    }

    private void getJson(String data) {
        try {
            JSONObject jsonObject=new JSONObject(data);
            JSONArray jsonArray=jsonObject.getJSONArray("data");
            for(int i=0;i<jsonArray.length();i++){
                JSONObject jsonData= (JSONObject) jsonArray.get(i);
                String name=jsonData.getString("name");
                String link=jsonData.getString("link");
                NavigationDataBean baseData=new NavigationDataBean();
                baseData.setName(name);
                baseData.setLink(link);
                mList.add(baseData);
                baseAdapter.notifyDataSetChanged();
            }
        } catch (JSONException e) {

        }
    }
}
